package com.ormbench.base.model;

import java.util.Date;

import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

/**
 * For auditing purposes every model object should have the following, hence so should every table
 */
@MappedSuperclass
public abstract class BaseModel {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name = "id", nullable = false)
	private Long id;
	public Long getId() { return this.id; }
	public void setId(Long id) { this.id = id; }

	@Basic
	@Column(name = "created_by", nullable = false, length = 64)
	private String createdBy;
	public String getCreatedBy() { return createdBy; }
	public void setCreatedBy(String createdBy) { this.createdBy = createdBy; }

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "created_date", nullable = false)
	private Date createdDate;
	public Date getCreatedDate() { return this.createdDate; }
	public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; }

	@Basic
	@Column(name = "updated_by", nullable = false, length = 64)
	private String updatedBy;
	public String getUpdatedBy() { return this.updatedBy; }
	public void setUpdatedBy(String updatedBy) { this.updatedBy = updatedBy; }

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "updated_date", nullable = false)
	private Date updatedDate;
	public Date getUpdatedDate() { return this.updatedDate; }
	public void setUpdatedDate(Date updatedDate) { this.updatedDate = updatedDate; }

	@Transient
	public int hashCode() {
		return this.getId().hashCode();
	}

	@Transient
	public boolean equals(Object o) {
		if (o == this) {
			return true;
		}
		if (o == null || !(o instanceof BaseModel)) {
			return false;
		}
		BaseModel model = (BaseModel) o;
		if (model.getId() == this.getId()) {
			return true;
		}
		return this.getId().equals(model.getId());
	}

	@Transient
	public String toString() {
	    return this.getClass().getSimpleName() + " ID = " + this.getId();
	}
}
